package edu.tyut.game.controller.circle;

import com.alibaba.fastjson2.JSON;
import edu.tyut.game.model.Comment;
import edu.tyut.game.model.FriendCircle;
import edu.tyut.game.model.Result;
import edu.tyut.game.service.FriendCircleService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ResourceUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import static edu.tyut.game.utils.ImageUtil.saveImage;

@Slf4j
@RestController
@RequestMapping("/circle")
public class FriendCircleController {

    private FriendCircleService friendCircleService;

    @Autowired
    public void setFriendCircleService(FriendCircleService friendCircleService) {
        this.friendCircleService = friendCircleService;
    }

    // // 发送朋友圈
    // @PostMapping("/postFriendCircle")
    // public Result<Boolean> postFriendCircle(@RequestBody FriendCircle friendCircle){
    //     friendCircleService.postFriendCircle(friendCircle);
    //     return Result.success("成功发布朋友圈", true);
    // }

    // 删除朋友圈
    @DeleteMapping("/deleteFriendCircle/{fcId}")
    public Result<Boolean> deleteFriendCircle(@PathVariable int fcId) {
        friendCircleService.deleteFriendCircle(fcId);
        return Result.success("成功删除朋友圈", true);
    }

    // 修改朋友圈
    @PostMapping("/updateFriendCircle")
    // 图片、内容
    public Result<Boolean> updateFriendCircle(@RequestParam("files") MultipartFile[] files, @RequestParam("content") String friendCircleJson) {
        log.info("更新朋友圈");
        FriendCircle friendCircle = toFriendCircle(files, friendCircleJson);
        // 保存到数据库中
        friendCircleService.editeFriendCircle(friendCircle);
        return Result.success("朋友圈修改成功成功", true);
    }

    private FriendCircle toFriendCircle(MultipartFile[] files, String friendCircleJson) {
        FriendCircle friendCircle = JSON.parseObject(friendCircleJson, FriendCircle.class);
        List<String> urls = new ArrayList<>();
        for (MultipartFile file : files) {
            urls.add(saveImage(file));
        }
        friendCircle.setImages(JSON.toJSONString(urls));
        return friendCircle;
    }

    // 查询朋友圈
    @GetMapping("/queryFriendCircle/{order}")
    public Result<List<FriendCircle>> queryFriendCircle(@PathVariable("order") String order) {
        return Result.success("获取朋友圈成功", friendCircleService.queryFriendCircle(order));
    }

    // 朋友圈点赞 + 1
    @GetMapping("/addFriendCircleLike/{fcId}")
    public Result<Boolean> addFriendCircleLike(@PathVariable int fcId) {
        friendCircleService.addFriendCircleLike(fcId);
        return Result.success("朋友圈点赞成功", true);
    }

    // 朋友圈点赞 - 1
    @GetMapping("/subFriendCircleLike/{fcId}")
    public Result<Boolean> subFriendCircleLike(@PathVariable int fcId) {
        friendCircleService.subFriendCircleLike(fcId);
        return Result.success("朋友圈取消点赞成功", true);
    }

    // 发表评论
    @PostMapping("/postComment")
    public Result<Boolean> postComment(@RequestBody Comment comment) {
        friendCircleService.postComment(comment);
        return Result.success("评论发表成功", true);
    }

    // 删除评论
    @GetMapping("/deleteComment/{id}")
    public Result<Boolean> deleteComment(@PathVariable int id) {
        friendCircleService.deleteComment(id);
        return Result.success("评论删除成功", true);
    }

    // 查询评论
    @GetMapping("/queryComment/{fcId}")
    public Result<List<Comment>> queryComment(@PathVariable int fcId) {
        List<Comment> comments = friendCircleService.queryComment(fcId);
        log.info(comments.toString());
        return Result.success("查询成功", comments);
    }

    @GetMapping("/addCommentLike/{commentId}")
    public Result<Boolean> addCommentLike(@PathVariable int commentId) {
        friendCircleService.addCommentLike(commentId);
        return Result.success("评论点赞成功", true);
    }

    @GetMapping("/subCommentLike/{commentId}")
    public Result<Boolean> subCommentLike(@PathVariable int commentId) {
        friendCircleService.subCommentLike(commentId);
        return Result.success("评论取消点赞成功", true);
    }

    // 上传朋友圈
    @PostMapping("/postFriendCircle")
    // 图片、内容
    public Result<Boolean> postFriendCircle(@RequestParam("files") MultipartFile[] files, @RequestParam("content") String friendCircleJson) {
        log.debug("postFriendCircle...");
        FriendCircle friendCircle = toFriendCircle(files, friendCircleJson);
        // 保存到数据库中
        friendCircleService.postFriendCircle(friendCircle);
        return Result.success("图片上传成功", true);
    }


}
